package com.ht.module.job.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ht.module.job.entity.JobInfo;
import com.ht.module.job.service.JobInfoService;
import com.ht.util.CommMethod;
import com.ht.util.JsonResult;
import com.ht.util.Pager;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 定时任务信息表，用于存储任务基本信息 前端控制器
 * </p>
 *
 * @author hejialun
 * @since 2025-08-11
 */
@Slf4j
@RestController
@RequestMapping("/job-info")
public class JobInfoController {


    @Autowired
    private JobInfoService jobInfoService;


    @Operation(summary = "分页查询")
    @GetMapping("/findPage")
    @PreAuthorize("hasAnyAuthority('JOB_INFO')")
    public JsonResult findPage(Pager<JobInfo> pager, JobInfo en) {
        return JsonResult.success(jobInfoService.findPage(pager, en));
    }


    @Operation(summary = "通过id查询")
    @GetMapping("/get-by-id/{id}")
    public JsonResult getById(@PathVariable(value = "id") String id) {
        return JsonResult.success(jobInfoService.getById(id));
    }

    @Operation(summary = "新增")
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('JOB_INFO_ADD')")
    public JsonResult add(@RequestBody @Valid JobInfo en) {
        CommMethod.beanCreate(en);
        try {
            jobInfoService.addOrUpdateJob(en);
        } catch (SchedulerException e) {
            if (BeanUtil.isNotEmpty(en)) {
                log.error("添加定时任务失败：{}", en.toString());
            }
            throw new RuntimeException(e);
        }
        return JsonResult.success();
    }


    @Operation(summary = "修改")
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('JOB_INFO_UPDATE')")
    public JsonResult updateById(@RequestBody @Valid JobInfo en) {
        CommMethod.beanUpdate(en);
        try {
            jobInfoService.addOrUpdateJob(en);
        } catch (SchedulerException e) {
            if (BeanUtil.isNotEmpty(en)) {
                log.error("修改定时任务失败：{}", en.toString());
            }
            throw new RuntimeException(e);
        }
        return JsonResult.success();
    }

    @Operation(summary = "通过id删除")
    @DeleteMapping("/delete-by-id/{id}")
    @PreAuthorize("hasAnyAuthority('JOB_INFO_DEL')")
    public JsonResult delete(@PathVariable(value = "id") String id) {
        jobInfoService.delById(id);
        return JsonResult.success();
    }

    @Operation(summary = "暂停定时任务")
    @PutMapping("/pause/{id}")
    @PreAuthorize("hasAnyAuthority('JOB_INFO_ADD')")
    public JsonResult pause(@PathVariable(value = "id") String id) {
        jobInfoService.pause(id);
        return JsonResult.success();
    }



    @Operation(summary = "运行定时任务")
    @PutMapping("/run/{id}")
    @PreAuthorize("hasAnyAuthority('JOB_INFO_ADD')")
    public JsonResult run(@PathVariable(value = "id") String id) {
        jobInfoService.run(id);
        return JsonResult.success();
    }


    @Operation(summary = "定时任务执行一次")
    @PutMapping("/runOnce/{id}")
    @PreAuthorize("hasAnyAuthority('JOB_INFO_ADD')")
    public JsonResult runOnce(@PathVariable(value = "id") String id) {
        jobInfoService.runOnce(id);
        return JsonResult.success();
    }

}
